package com.hanzidong.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.hanzidong.bean.Article;
import com.hanzidong.bean.Category;
import com.hanzidong.bean.Channel;
import com.hanzidong.bean.Collect;
import com.hanzidong.bean.User;
import com.hanzidong.service.ArticleService;
import com.hanzidong.service.ChannelService;
import com.hanzidong.utils.RandomUtil;

@Controller
@RequestMapping("my")
public class MyController {
	
	@Autowired
	private ChannelService channelService;
	
	@Autowired
	private ArticleService articleService;
	
	/**
	 * 跳转到个人中心首页
	 * @return
	 */
	@RequestMapping({"/","","index"})
	public String index() {
		
		
		return "my/index";
	}

	/**
	 * 跳转到发布页面，只有get请求才能访问的
	 * @return
	 */
	@GetMapping("publish")
	public String publish() {
		
		return "my/publish";
	}
	
	
	/**
	 * 查询所有的栏目数据
	 * @return
	 */
	@RequestMapping("getChannels")
	@ResponseBody
	public List<Channel> getChannels(){
		
		//查询所有的栏目数据
		List<Channel> list = channelService.getChannels();
		
		return list;
	}

	/**
	 * 根据栏目的id获取，该栏目下所有的分类
	 * @param channelId
	 * @return
	 */
	@RequestMapping("getCategoryByChannelId")
	@ResponseBody
	public List<Category> getCategoryByChannelId(Integer channelId){
		
		return channelService.getCategoryByChannelId(channelId);
	}
	
	/**
	 * 发布文章，接收图片
	 * @param article
	 * @param file
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("publish")
	@ResponseBody
	public boolean publish(Article article,MultipartFile file,HttpSession session) throws IllegalStateException, IOException {
		
		//处理图片
		if(file != null && file.getSize() > 0) {
			//获取原文件名
			String filename = file.getOriginalFilename();
			
			//使用uuid生成随机文件名
			filename = RandomUtil.uuid() + filename.substring(filename.lastIndexOf("."));
			
			//获取目标文件
			File f = new File("d:/img/"+filename);
			
			//复制文件
			file.transferTo(f);
			
			//将文件名存入到实体类中
			article.setPicture("/img/" + filename);
		}
		
		//设置文章其他字段
		User user = (User) session.getAttribute("LOGIN_USER");
		article.setUserId(user.getId());//从session中获取对象
		
		article.setHits(0);//点击量
		article.setHot(0);//是否热门
		article.setStatus(0);//0,刚发布,1审核通过,-1 审核未通过
		article.setDeleted(0);//0:正常,1:删除
		article.setCreated(new Date());//文章创建日期
		article.setContentType(0);//文章类型: 0:html 1:json
		article.setComments(0);//评论数量
		
		//将文章存入到数据库
		boolean result = articleService.add(article);
		
		return result;
	}
	
	/**
	 * 查询当前用户的所有文章的列表
	 * @param m
	 * @param article
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("articles")
	public String articles(Model m, HttpSession session,
			@RequestParam(defaultValue = "1")Integer pageNum,
			@RequestParam(defaultValue = "5")Integer pageSize) {
		
		//从session中获取用户
		User user = (User) session.getAttribute("LOGIN_USER");
		
		//查询数据
		PageInfo<Article> info = articleService.listByUser(pageNum,pageSize,user);
		
		//将数据发送到jsp页面
		m.addAttribute("info", info);
		
		return "my/articles";
	}
	
	/**
	 * 根据文章id查询文章数据
	 * @param id
	 * @return
	 */
	@RequestMapping("detail")
	@ResponseBody
	public Article detail(Integer id) {
		
		return articleService.getDetailById(id);
	}
	
	/**
	 * 添加收藏
	 * @param id
	 * @return
	 */
	@RequestMapping("addCollect")
	@ResponseBody
	public Collect addCollect(Collect collect,HttpSession session) {
		//获取登录用户
		User user = (User) session.getAttribute("LOGIN_USER");
		
		//设置用户
		collect.setUserId(user.getId());
		
		//设置路径
		collect.setUrl("http://localhost/detail?articleId="+collect.getArticleId());
		
		//设置时间
		collect.setCreated(new Date());
		
		//添加收藏
		articleService.addCollect(collect);
		
		return collect;
		
	}
	/**
	 * 取消收藏
	 * @param id
	 * @return
	 */
	@RequestMapping("delCollect")
	@ResponseBody
	public boolean addCollect(Collect collect) {
		
		//添加收藏
		return articleService.delCollect(collect);
		
	}
	/**
	 * 查看收藏
	 * @param id
	 * @return
	 */
	@RequestMapping("getCollectByArticleId")
	@ResponseBody
	public Collect getCollectByArticleId(Collect collect,HttpSession session) {
		
		//获取登录用户
		User user = (User) session.getAttribute("LOGIN_USER");
		
		//设置用户
		collect.setUserId(user.getId());
		
		return articleService.getCollectByArticleId(collect);
	}
	/**
	 * 获取用户的所有收藏
	 * @param m
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("collect")
	public String collect(Model m,HttpSession session,
			@RequestParam(defaultValue = "1")Integer pageNum, 
			@RequestParam(defaultValue = "3")Integer pageSize) {
		
		//获取登录后的user对象
		User user = (User) session.getAttribute("LOGIN_USER");
		
		//根据用户id查询其发布的所有文章
		PageInfo<Collect> info = articleService.getCollect(pageNum,pageSize,user);
		
		//将数据传到jsp页面
		m.addAttribute("info", info);
		
		return "my/collect";
	}
}


